package cn.itcast.n4;/**
 * @author yd
 * @date 2022/3/17 21:00
 * @version 1.0
 */

import cn.itcast.utils.Sleeper;
import lombok.extern.slf4j.Slf4j;

import static cn.itcast.utils.Sleeper.sleep;

@Slf4j(topic = "w.TestWaitNotify")
public class TestWaitNotify {

    final static Object obj = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            log.debug("执行");
            synchronized (obj){
                try {
                    obj.wait();//让线程在obj上一直等待下去
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("其它代码。。。。");
            }

        }, "t1").start();
        new Thread(() -> {
            log.debug("执行");
            synchronized (obj){
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                log.debug("其它代码。。。。");
            }

        }, "t2").start();

        //主线程两秒后执行
        sleep(2);
        log.debug("唤醒 obj 上其它线程");
        synchronized (obj){
            //obj.notify();
            obj.notifyAll();
        }
    }

}
